/*	******************************************
 File:			StateGame.cpp
 Author:		Adnan Srna, Peter Kober, Ivo Havranek
 Purpose:		AugmentedReality 2
 (up)Date:		March/April 2010
 Descr.:		Game State, demo game for augmented presentation
 Origin:		Salzburg University of Applied Science
 ******************************************	*/

#define	MAX_LIVES	3

#include "VoiceOverEyeP.h"

VoiceOverEyeP::VoiceOverEyeP(){
	ofAddListener(ofEvents.keyPressed, this, &VoiceOverEyeP::keyPressed);
	ofAddListener(ofEvents.keyReleased, this, &VoiceOverEyeP::keyReleased);

	//_thread = new ofxThread[2];
}

VoiceOverEyeP::~VoiceOverEyeP(){
	ofRemoveListener(ofEvents.keyPressed, this, &VoiceOverEyeP::keyPressed);
	ofRemoveListener(ofEvents.keyReleased, this, &VoiceOverEyeP::keyReleased);
}

//--------------------------------------------------------------
void VoiceOverEyeP::setup(){

	input = speed = ofPoint(0, 1);
	time = ofGetElapsedTimef();
	randomTime = 2;
	lives = MAX_LIVES;
	initAssets();
	ofSetFrameRate(60);

	soundTool.setup();
	soundTool.amplitude = 0;
	
	threadFace.start();
	mydebug = true;
}

//--------------------------------------------------------------
void VoiceOverEyeP::update(){
	
		// Street updates
	speed.y = ofLerp(speed.y, input.y, 0.01);		// smoothing
	Street.position.y += speed.y;
	if (Street.position.y + ofGetHeight()/2 >= ofGetHeight()) 
		Street.position.y = 0;
	
		// Car updates
	speed.x = ofLerp(speed.x, input.x, 0.05);		// smoothing
	Car.position.x += speed.x;
	if (Car.position.x <= Car.image.width) {
		Car.position.x = Car.image.width + 1;
		input.x = speed.x = 0;
	}
	if (Car.position.x >= ofGetWidth() - Car.image.width) {
		Car.position.x = ofGetWidth() - Car.image.width - 1;
		input.x = speed.x = 0;
	}
		
		// Fishy updates
	if (Fishy.position.y < ofGetHeight() + Fishy.image.height/2) {
		Fishy.position.y += speed.y;
	}
	else {
		if (time == 0)
			time = ofGetElapsedTimef();
		if ((int)ofGetElapsedTimef() - time >= randomTime) {
			randomTime = ofRandom(0, 3);
			Fishy.position = ofPoint(ofRandom(Fishy.image.width , ofGetWidth() - Fishy.image.width), -Fishy.image.height/2);
			time = 0;
		}
	}
	
		// collision check
	if (carCrash()) {
		Fishy.position.y = ofGetHeight() + Fishy.image.height/2;
		lives--;
		if (time == 0)
			time = ofGetElapsedTimef();
	}	

	
	soundTool.update();
	//faceTool.update();

	input.y = 1+soundTool.amplitude;
	//input.x = -(160 - faceTool.amplitude) / 50;
	input.x = -(160 - threadFace.faceTool.amplitude) / 50;
	//cout<<input.x<<endl;
}

//--------------------------------------------------------------
void VoiceOverEyeP::draw(){
	
	/* Setting the anchorpoint at the lower middle
		+-----------+			  ^
		|			|			  | +y
		|			|			  |
		+-----X-----+		<-----+----->
								   -x	   +x
	 
		ofTranslate(ofGetWidth() / 2, ofGetHeight(), 0);
		ofScale(1, -1, 1); */
	
	ofEnableAlphaBlending();
	drawStreet();
	if (Fishy.position.y < ofGetHeight() + Fishy.image.height/2)
		Fishy.image.draw(Fishy.position.x - Fishy.image.width/2, Fishy.position.y - Fishy.image.height/2);
	ofSetColor(255, 255, 255);
	string currentSpeed = ofToString(int((speed.y / 8) * 160)) + " ka-em/ha";
	//ofDrawBitmapString("Lives:", 25, 15);
	ofDrawBitmapString(currentSpeed, 10, ofGetHeight() - 10);
	for (int var = 0; var < lives; var++) {
			Car.image.draw((var + 1)*Car.image.width/2, 25, 20, 30);
	}
	
	/*ofPushMatrix();
		ofTranslate(350, 1, 0);
		ofScale(0.3, 0.3, 1);
		faceTool.draw();
	ofPopMatrix();*/
	
	//cout << Fishy.position.x << " , " << Fishy.position.y << "      :" << Fishy.image.width << " , " << Fishy.image.height << endl;
	//cout << Car.position.x << " , " << Car.position.y << "      :" << Car.image.width << " , " << Car.image.height << endl << endl;

	if(mydebug){
		ofTranslate(350, 1, 0);
		ofScale(0.3, 0.3, 1);
		threadFace.draw();
	}
}

//--------------------------------------------------------------
void VoiceOverEyeP::keyPressed(ofKeyEventArgs &arg)
{
	switch(arg.key){
		case OF_KEY_LEFT:
			input.x = -3;			// <------ HeAd input
			break;
		case OF_KEY_RIGHT:
			input.x = 3;			// <------ HeAd input
			break;
		case OF_KEY_UP:
			input.y = 8;			// <------ VoIcE input
			break;
		case 's':
			threadFace.startThread();
			break;
		case 'x':
			threadFace.stopThread();
			break;
		case 'q':
			ofAppGlutWindow::exitApp();
			break;
		case 'd':
			mydebug = mydebug^1;
			break;
	}
}

//--------------------------------------------------------------
void VoiceOverEyeP::keyReleased(ofKeyEventArgs &arg)
{
	switch (arg.key) {
		case OF_KEY_LEFT:
		case OF_KEY_RIGHT:
			input.x = 0;			// not 
			break;
		case OF_KEY_UP:
			input.y = 1;
			break;
	}
}


//--------------------------------------------------------------
void VoiceOverEyeP::checkBoundary(float &x, float &y){
	if (x >= ofGetWidth())		x = ofGetWidth() - 1;
	if (x <= 0)					x = 1;
	if (y >= ofGetHeight())		y = ofGetHeight() - 1;
	if (y <= 0)					y = 1;
}

//--------------------------------------------------------------
void VoiceOverEyeP::initAssets(){
	Car.image.loadImage("images/car.png");
	Car.position = ofPoint(ofGetWidth()/2, ofGetHeight() - 80);
	
	Street.image.loadImage("images/street.png");
	Street.position = ofPoint(ofGetWidth()/2, ofGetHeight()/2);
	
	Fishy.image.loadImage("images/fishy.png");
	Fishy.position = ofPoint(ofGetWidth()/2, ofGetHeight() + Fishy.image.height/2);
}

//--------------------------------------------------------------
void VoiceOverEyeP::drawStreet(){
		// draw the street twice to fake infinity
	Street.image.draw(Street.position.x - ofGetWidth()/2, Street.position.y - ofGetHeight()/2, ofGetWidth(), ofGetHeight());
	Street.image.draw(Street.position.x - ofGetWidth()/2, Street.position.y + ofGetHeight()/2, ofGetWidth(), ofGetHeight());
	ofScale(1, -1, 1);
	Car.image.draw(Car.position.x - Car.image.width/2, -Car.position.y - Car.image.height/2, Car.image.width, Car.image.height);
	ofScale(1, -1, 1);
}

//--------------------------------------------------------------
bool VoiceOverEyeP::carCrash() {

	if	(((Car.position.x + Car.image.width/2) >= (Fishy.position.x - Fishy.image.width/2)) && ((Car.position.x - Car.image.width/2) <= (Fishy.position.x + Fishy.image.width/2))
	&&	((Car.position.y + Car.image.height/2) >= (Fishy.position.y - Fishy.image.height/2)) && ((Car.position.y - Car.image.height/2) <= (Fishy.position.y + Fishy.image.height/2)))
		return true;
	return false;
}
